package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysResource;
import org.sky.sys.entity.SysRole;
import org.sky.sys.service.SysRoleService;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.TreeStru;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.LogUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysRoleController
 * @ProjectName springboot-platform
 * @Description: TODO{角色管理}
 * @date 2019/3/8 18:45
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/sys/SysRole")
public class SysRoleController {
    Logger log = LogUtils.getLogger(SysRoleController.class);
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 打开角色管理页面
     * @return
     */
    @GetMapping(value = "/initSysRoleMngPage")
    public String initSysRoleMngPage(){
        return "sys/role/sysrolemng";
    }

    /**
     * 角色数据分页查询
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysRoleByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String selectSysRoleByPage(
            HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysRole.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysRoleService.selectSysRoleByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }

    /**
     * 打开角色添加页面
     * @return
     */
    @GetMapping(value = "/initSysRoleAddPage")
    public String initSysRoleAddPage(){
        return "sys/role/sysroleedit";
    }

    /**
     * 打开角色修改页面
     * @return
     */
    @GetMapping(value = "/initSysRoleEditPage")
    public String initSysRoleEditPage(){
        return "sys/role/sysroleedit";
    }

    /**
     * 保存{新增/修改}角色
     * @param request
     * @return
     */
    @PostMapping(value = "/saveAddEditSysRole",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysRole(
            HttpServletRequest request){
        Result result = null;
        try {
            SysRole edit = RequestUtils.getEntityBean(request,SysRole.class);
            sysRoleService.saveAddEditSysRole(edit);
            result = Result.success("保存成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }

    /**
     *根据主键查询角色
     **/
    @GetMapping(value = "/getSysRoleById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysRolerById(
            @PathVariable Long id){
        SysRole bean = sysRoleService.getSysRoleById(id);
        return JsonUtils.obj2json(bean);
    }
    @PostMapping(value = "/delSysRole",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysRole(
            HttpServletRequest request){
        Result result=null;
        try {
            String delRows=request.getParameter("delRows");
            List<SysRole> de=JsonUtils.json2list(delRows, SysRole.class);
            sysRoleService.delSysRoleById(de);
            result = Result.success("删除成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 角色资源树页面
     * @return
     */
    @RequestMapping(value = "/initRoleResMngPage", method = { RequestMethod.GET })
    public String initRoleResMngPage() {
        return "sys/role/roleresmng";
    }

    /**
     * 查询角色资源树
     * @param roleCode
     * @return
     */
    @PostMapping(value = "/selectRoleResTree/{roleCode}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectRoleResTree(@PathVariable String roleCode){
        List<TreeStru> list = sysRoleService.selectRoleResTree(roleCode);
        return JsonUtils.obj2json(list);
    }

    /**
     * 保存角色资源
     * @param request
     * @return
     */
    @PostMapping(value = "/saveRoleResource",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveRoleResource(
            HttpServletRequest request){
        Result result=null;
        try {
            String role = request.getParameter("role");
            String resource = request.getParameter("resource");
            SysRole sr = JsonUtils.json2pojo(role, SysRole.class);
            List<SysResource> solist = JsonUtils.json2list(resource, SysResource.class);
            sysRoleService.saveRoleResource(sr,solist);
            result = Result.success("保存成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
}
